<?php
namespace Tbf\Bundle\TestBundle\Listener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class TextExceptionListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array(
            KernelEvents::EXCEPTION=>array('onException',-127),
        );
    }
    //处理异常
    public function onException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();
        $msg = $exception->getMessage();
        $output = $msg."\n";
        $output .= $exception->getTraceAsString();
        $event->setResponse(new Response($output),500);
        $event->stopPropagation();
    }
}
